home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Devices / Qwertytunes / BigEasyTextish.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-03  |  7.1 KB  |  434 lines  |  [TEXT/KAHL]

  1. /*
  2.                 File:        BigEasyTextish.c
  3.             
  4.                 Contains:    xxx put contents here xxx
  5.             
  6.                 Written by:    xxx put writers here xxx
  7.             
  8.                 Copyright:    © 1990-1992 by Apple Computer, Inc., all rights reserved.
  9.             
  10.     This file is used in these builds: Warhol
  11.  
  12.                 Change History (most recent first):
  13.             
  14.          <9>      1/7/93    dvb        New functions (TruncateString).
  15.          <8>     1/20/92    dvb        This kind of FixMath is KRAZEE!
  16.          <7>    11/12/91    dvb        General fixation
  17.          <6>     11/4/91    JB        Use fixmath instead of floats
  18.          <5>      6/3/91    dvb        Just Hackin'.
  19.          <4>     5/23/91    PH        THINK C 5
  20.          <3>     4/25/91    JB        Changing to new THINK_C interface files
  21.          <2>    11/16/90    dvb        Remove drawcstring
  22.         <1>        11/16/90    dvb        Check In! Fresh after Camplejohn Soup!
  23.             
  24.                 To Do:
  25.             */
  26.  
  27. /* file: BigEasyTextish.c
  28.  *
  29.  * Started 13 July 1989, more or less.
  30.  *
  31.  * A set of routines for converting and
  32.  * displaying textish things on the Mac.
  33.  *
  34.  */
  35.  
  36.  
  37.  
  38. /************************************
  39. * Inclusions
  40. ************************************/
  41.  
  42. #define BigEasyTextish
  43.  
  44. #include <QuickDraw.h>
  45. #include <Packages.h>
  46. #include <Memory.h>
  47. #include <FixMath.h>
  48.  
  49. #include "BigEasyTextish.h"
  50.  
  51.  
  52. /************************************
  53. * Limits
  54. ************************************/
  55.  
  56. /************************************
  57. * Types and globals
  58. ************************************/
  59. static char dHexChars[] = "0123456789ABCDEF";
  60.  
  61. /************************************
  62. * Routines
  63. ************************************/
  64. void AnyBaseToPString(long n,short b,short g,StringPtr c)
  65. /*
  66.   * Convert 32 bit number n to
  67.   *  string c, in number base b.
  68.   * g is the format: 0 - any length signed
  69.   * positive: unsigned with leading zeros
  70.   * to width g.
  71.   */
  72.     {
  73.     register short charCount;
  74.     register StringPtr w;
  75.     register unsigned long x;        /* unsigned number for divs    */
  76.  
  77.     if(b<2 || b>16)                /* a ridiculous number base?    */
  78.         {
  79.         c[0] = 0;                /* mark as empty string        */
  80.         return;                /* bye.                    */
  81.         }
  82.  
  83.     if(g == 0)
  84.         {
  85.         charCount = 1;            /* initially, 1 char long        */
  86.  
  87.         if(n < 0)                /* number negative?        */
  88.             {
  89.             charCount++;        /* one more char            */
  90.             n = -n;                /* make it positive            */
  91.             c[1] = '-';            /* drop in a minus sign        */
  92.             if(n < 0)
  93.                 n --;            /* most negative? pin it. */
  94.             }
  95.  
  96.         x = n;                /* find out how many digits    */
  97.         while(x /= b)
  98.             charCount++;
  99.  
  100.         c[0] = charCount;        /* number of digits, plus sign    */
  101.         w = c+c[0];            /* we'll walk backwards        */
  102.         x = n;
  103.         do
  104.             {
  105.             *w-- = dHexChars[x%b];
  106.             x /= b;
  107.             } while(x);
  108.         }
  109.     else if(g > 0)
  110.         {                    /* leading zero format        */
  111.         c[0] = g;            /* poke in number of chars    */
  112.         x = n;                /* get our number unsigned    */
  113.         w = &c[g];            /* point to end of string        */
  114.  
  115.         while(g--)
  116.             {
  117.             *w-- = dHexChars[x%b];
  118.             x /= b;
  119.             }
  120.         }
  121.     else if(g < 0)
  122.         {                    /* leading space format   */
  123.         g = -g;
  124.         c[0] = g;                /* poke in number of chars    */
  125.         x = n;                /* get our number unsigned    */
  126.         w = &c[g];            /* point to end of string        */
  127.         
  128.         while(g--)
  129.             {
  130.             *w-- = dHexChars[x%b];
  131.             x /= b;
  132.             }
  133.         g = c[0]-1;
  134.         w = &c[1];
  135.         while(g--)
  136.             {
  137.             if (*w == '0')
  138.                 *w++ = 0xca;
  139.             else
  140.                 break;
  141.             }
  142.         }
  143.     else                    /* negative format passed    */
  144.         c[0] = 0;
  145.     }
  146.  
  147. void DrawNum(n)
  148. /*
  149.   * Draw number n as a signed long, in decimal
  150.   */
  151.     long n;
  152.     {
  153.     Str31 c;
  154.  
  155.     AnyBaseToPString(n,10,0,c);
  156.     DrawString((StringPtr)c);
  157.     }
  158.  
  159. long dTens[] =
  160.     {
  161.     1,
  162.     10,
  163.     100,
  164.     1000,
  165.     10000,
  166.     100000,
  167.     1000000,
  168.     10000000,
  169.     100000000,
  170.     1000000000
  171.     };
  172.  
  173. void DrawFixedPoint(long n,short g,short p)
  174. /*
  175.   * Draw number n in fixed point, with
  176.   * g decimal places, with p fractional
  177.   * binary places.
  178.   */
  179.     {
  180.     Str31 c;
  181.     long x;        /* This should float to avoid overflows, but JB says, No! */
  182.     Str255 s;
  183.  
  184.     s[0] = 0;
  185.  
  186.     if( n < 0 )
  187.         {
  188.         ConcatenatePStrings(s,"\p-");
  189.         n = -n;
  190.         if(n < 0)
  191.             n--;        /* pin most negative */
  192.         }
  193.  
  194.     AnyBaseToPString(n>>p,10,0,c);
  195.     ConcatenatePStrings(s,c);
  196.     ConcatenatePStrings(s,"\p.");
  197.  
  198.     if(g>9)
  199.         g = 9;
  200.  
  201.     if(g)
  202.         {
  203.         x = (n & ((1L<<p)-1) );
  204.         x *= dTens[g];
  205.         x /= (x,1L<<p);
  206.  
  207.         AnyBaseToPString(x,10,g,c);
  208.         ConcatenatePStrings(s,c);
  209.         }
  210.  
  211.     DrawString(s);
  212.     }
  213.  
  214.  
  215. void DrawFixed(long n,short g)
  216. /*
  217.   * Draw number n in fixed point, with
  218.   * g decimal places.
  219.   */
  220.     {
  221.     DrawFixedPoint(n,g,16);
  222.     }
  223.  
  224. void DrawFixedPointJustified(long n,short g,short p,short h)
  225. /*
  226.   * Draw number n in fixed point, with
  227.   * g decimal places, with p fractional
  228.   * binary places.
  229.   */
  230.     {
  231.     Str255 s;
  232.     Str31 c;
  233.     long x;
  234.  
  235.     s[0] = 0;
  236.  
  237.     if( n < 0 )
  238.         {
  239.         ConcatenatePStrings(s,"\p-");
  240.         n = -n;
  241.         if(n < 0)
  242.             n --;            /* most negative? pin it. */
  243.         }
  244.  
  245.     AnyBaseToPString(n>>p,10,-h,c);
  246.     ConcatenatePStrings(s,c);
  247.     ConcatenatePStrings(s,"\p.");
  248.  
  249.     if(g>9)
  250.         g = 9;
  251.  
  252.     if(g)
  253.         {
  254.         
  255.         x = (n & ((1L<<p)-1) );
  256.         x *= dTens[g];
  257.         x /= (x,1L<<p);
  258.  
  259.         AnyBaseToPString(x,10,g,c);
  260.         ConcatenatePStrings(s,c);
  261.         }
  262.  
  263.     DrawString(s);
  264.     }
  265.  
  266.  
  267. void DrawFixedJustified(long n,short g,short h)
  268. /*
  269.   * Draw number n in fixed point, with
  270.   * g decimal places.
  271.   */
  272.     {
  273.     DrawFixedPointJustified(n,g,16,h);
  274.     }
  275.  
  276.  
  277.  
  278. void DrawFrac(long n,short g)
  279. /*
  280.   * Draw number n as a frac (2.30), with
  281.   * g decimal places.
  282.   */
  283.     {
  284.     DrawFixedPoint(n,g,30);
  285.     }
  286.  
  287. void DrawHexLong(unsigned long n)
  288. /*
  289.   * Draw an 8-digit hex number
  290.   * with leading zeroes
  291.   */
  292.     {
  293.     Str31 c;
  294.  
  295.     AnyBaseToPString(n,16,8,c);
  296.     DrawString((StringPtr)c);
  297.     }
  298.  
  299. void DrawHexShort( unsigned short n)
  300. /*
  301.   * Draw a 4-digit hex number
  302.   * with leading zeroes
  303.   */
  304.     {
  305.     Str31 c;
  306.  
  307.     AnyBaseToPString(n,16,4,c);
  308.     DrawString((StringPtr)c);
  309.     }
  310.  
  311. void CToPString(register char *c,register StringPtr p)
  312.     {
  313.     register short i;
  314.     register StringPtr pWalker;
  315.  
  316.     i = 0;
  317.     pWalker = p+1;
  318.     while(*pWalker++ = *c++)
  319.         i++;
  320.     *p = i;
  321.     }
  322.  
  323. void DrawCR(void)
  324. /*
  325.   * Bump penposition down a line, to
  326.   * gLeftMargin, and +gLineHeight.
  327.   */
  328.     {
  329.     Point p;
  330.  
  331.     GetPen(&p);
  332.     p.h = gLeftMargin;
  333.     p.v += gLineHeight;
  334.     MoveTo(p.h,p.v);
  335.     }
  336.  
  337.  
  338.  
  339. short CStringWidth(char *c)
  340. /*
  341.   * Do a StringWidth on a C string
  342.   */
  343.     {
  344.     Str255 p;
  345.  
  346.     CToPString(c,p);
  347.     return StringWidth((StringPtr)p);
  348.     }
  349.  
  350.  
  351. void CopyPString(StringPtr dest,StringPtr src)
  352.     {
  353.     BlockMove(src,dest,(*src) + 1);
  354.     }
  355.  
  356.  
  357. void ConcatenatePStrings(StringPtr dest,StringPtr src)
  358.     {
  359.     BlockMove(src+1,dest+1+(*dest),*src);
  360.     *dest += *src;
  361.     }
  362.  
  363.  
  364. void DrawStringRight(StringPtr s)
  365. /*
  366.  * Draw the string from the current pen position
  367.  * off to the left; leave the pen where it started.
  368.  */
  369.     {
  370.     Point po;
  371.  
  372.     GetPen(&po);
  373.     Move(-StringWidth(s),0);
  374.     DrawString(s);
  375.     }
  376.  
  377. void DrawStringCenter(StringPtr s)
  378. /*
  379.  * Draw the string centered at
  380.  * the current pen position; 
  381.  * leave the pen where it started.
  382.  */
  383.     {
  384.     Point po;
  385.  
  386.     GetPen(&po);
  387.     Move(-StringWidth(s)/2,0);
  388.     DrawString(s);
  389.     MoveTo(po.h,po.v);
  390.     }
  391.  
  392. void DrawStringLeft(StringPtr s)
  393. /*
  394.  * Draw the string from the current pen position
  395.  * off to the right; leave the pen where it started.
  396.  */
  397.     {
  398.     Point po;
  399.  
  400.     GetPen(&po);
  401.     DrawString(s);
  402.     MoveTo(po.h,po.v);
  403.     }
  404.  
  405.  
  406. void TruncateString(StringPtr s, short width)
  407. /*
  408.  * Draw the string limited to the width passed,
  409.  * Truncated if necessary with a trailing “…”.
  410.  */
  411.     {
  412.     while(StringWidth(s) > width)
  413.         {
  414.         s[0] --;
  415.         s[s[0]] = '…';
  416.         }
  417.     }
  418.  
  419.  
  420. void DrawStringTruncated(StringPtr s, short width)
  421. /*
  422.  * Draw the string limited to the width passed,
  423.  * Truncated if necessary with a trailing “…”.
  424.  */
  425.     {
  426.     Str255 s2;
  427.  
  428.     CopyPString(s2,s);
  429.     TruncateString(s2,  width);
  430.     DrawString(s2);
  431.     }
  432.  
  433.  
  434.